package org.example.project3.Service;

import org.example.project3.DTO.CategoryDTO;
import org.example.project3.Entity.Category;
import org.example.project3.Mapper.AcontentsMapper;
import org.example.project3.Mapper.CategoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    public List<Category> getCategory(){
        List<Category> categoryList = categoryMapper.getCategory();
        return categoryList;
    }

    @Transactional
    public void add(CategoryDTO dto){
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.add(category);
    }
    @Transactional
    public void delete(Integer id){
        categoryMapper.delete(id);
    }
    @Transactional
    public void update(CategoryDTO dto){
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

}
